﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace QuickExcel
{
    public partial class editBookmark : Form
    {
        public editBookmark()
        {
            InitializeComponent();
        }

        private void editBookmark_Load(object sender, EventArgs e)
        {
            load_listbox();
        }

        private void load_listbox()
        {
            listBookmarks.Items.Clear();

            TextReader tw = new StreamReader("c:\\Program Files\\QuickExcel\\test.txt");
            String line;
            while ((line = tw.ReadLine()) != null)
            {
                String[] parts = line.Split('-');
                listBookmarks.Items.Add(parts[0]);
            }
            lblFileName.Text = "";
            lblPathName.Text = "";
            lblSheetName.Text = "";
            lblRangeName.Text = "";
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdDeleteAll_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("c:\\Program Files\\QuickExcel\\test.txt", FileMode.Create);
            StreamWriter tw = new StreamWriter(fs);

            tw.WriteLine("");

            tw.Close();
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (listBookmarks.SelectedItems.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("Please select the bookmark from the list to delete");
                return;
            }
            string fileBookmark= "c:\\Program Files\\QuickExcel\\test.txt";
            string tempBookmark= "c:\\Program Files\\QuickExcel\\temp.txt";
            string bookmark = listBookmarks.SelectedItem.ToString();
            string line;

            TextReader sr = new StreamReader(fileBookmark);
            TextWriter temp_tw = new StreamWriter(tempBookmark);
            
            while ((line = sr.ReadLine()) != null)
            {
                String[] words = line.Split('-');
                if (bookmark != words[0])
                  temp_tw.WriteLine(line);
            }

            temp_tw.Close();
            sr.Close();

            File.Delete(fileBookmark);
            File.Move(tempBookmark, fileBookmark);

            System.Windows.Forms.MessageBox.Show("Bookmark Deleted Successfully");
            load_listbox();
        }

        private void listBookmarks_SelectedIndexChanged(object sender, EventArgs e)
        {
            update_details();
        }
        private void update_details()
        {
            string fileBookmark = "c:\\Program Files\\QuickExcel\\test.txt";
            string bookmark = "";
            try
            {
                bookmark = listBookmarks.SelectedItem.ToString();
            }
            catch
            { }

            string line;

            TextReader sr = new StreamReader(fileBookmark);

            while ((line = sr.ReadLine()) != null)
            {
                String[] words = line.Split('-');
                if (bookmark != words[0])
                {
                    lblFileName.Text = words[1];
                    lblPathName.Text = words[2];

                    if (words.Count() > 3)
                        lblSheetName.Text = words[3];

                    else
                    {
                        lblSheetName.Text = "";
                        lblRangeName.Text = "";
                    }

                    if (words.Count() > 4)
                    {
                        if (words[4] != null)
                            lblRangeName.Text = words[4];

                        else
                            lblRangeName.Text = "";
                    }
                }
            }

            sr.Close();
        }

        private void cmdRename_Click(object sender, EventArgs e)
        {
            if (listBookmarks.SelectedItems.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("No Bookmark Selected");
                return;
            }
            string fileBookmark = "c:\\Program Files\\QuickExcel\\test.txt";
            string tempBookmark = "c:\\Program Files\\QuickExcel\\temp.txt";
            string bookmark = listBookmarks.SelectedItem.ToString();
            string line;
            string newBookmarkName = Globals.ThisAddIn.Application.InputBox("Please Put the new name:", "Rename Bookmark");
            string renamedBookmark=newBookmarkName + "-";
            if (newBookmarkName == "")
                return;

            TextReader sr = new StreamReader(fileBookmark);
            TextWriter temp_tw = new StreamWriter(tempBookmark);

            while ((line = sr.ReadLine()) != null)
            {
                String[] words = line.Split('-');

                for (int i = 1; i < words.Count(); i++)
                    renamedBookmark = renamedBookmark + words[i] + "-";

                if (bookmark != words[0])
                    temp_tw.WriteLine(line);
                else
                    temp_tw.WriteLine(renamedBookmark);
            }

            temp_tw.Close();
            sr.Close();

            File.Delete(fileBookmark);
            File.Move(tempBookmark, fileBookmark);

            this.Focus();
            load_listbox();
        }
    }
}